//
//  VideoViewController.swift
//  Amicool
//
//  Created by Mac  on 2017/6/5.
//  Copyright © 2017年 Mac . All rights reserved.
//

import UIKit

private let reuseIdentifier = "videocell"

class VideoViewController: UICollectionViewController {

    
    var VideoList:NSArray!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
       // self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
      getVideoList()
        // Do any additional setup after loading the view.
    }
    func  getVideoList(){
        let parameters:NSDictionary = ["SessionID":AMNetworkMngTool.loginReturnModel!.sessionid!,"mod":"video","page":"1"]
        
        AMNetworkMngTool.shared.AMNetwork_GetVideoList(parameters) { (videoList) in
            if videoList != nil {
                DispatchQueue.main.async {
                    self.VideoList = videoList
                    self.collectionView?.reloadData()
                }
            }
            else{
                //提示，获取视频列表失败
            }
        }
    
    
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        
        if self.VideoList != nil
        {
            return VideoList.count
        }
        else {
            return 1
        }
     
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! VideoCell
    
        // Configure the cell
        if self.VideoList != nil{
            let model = self.VideoList[indexPath.row] as! VideoReturnJson
            cell.title.text = model.name
            cell.author.text = model.author
            if model.thumb != nil {
                let imgUrlPre = "http://amicool.neusoft.edu.cn/Uploads/"
                let imgUrl = URL(string: imgUrlPre + model.thumb!)
             let imgData = try! Data(contentsOf: imgUrl!)
                cell.img.image = UIImage(data: imgData)?.scaleImageToSize(CGSize(width: 80, height: 60))
            }
            else {
                cell.img.image = UIImage(named: "placeholder.png")
            }
        }
        return cell
    }

    // MARK: UICollectionViewDelegate

    
}
